# -*- coding: utf-8 -*-
import re

from bs4 import BeautifulSoup


html_doc = '''
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div class="content">
        <h1 class='h1'>一级标题</h1>
        <b>加粗</b>
        <p>段落</p>
        <span>内容</span>
        <a id='link' href='www.google.com' class='link'>link</a>
        <a id='back' href='www.baidu.com'>back</a>
    </div>
</body>
</html>
'''
parser = 'html.parser'
# 方式一：解析一个html文件
beautiful_soup = BeautifulSoup(open("index.html", 'rb'))
# 方式二：解析html字符串
# beautiful_soup = BeautifulSoup(html_doc, parser)

print(beautiful_soup)
print("获取html的title标签：", beautiful_soup.title)
print("获取html的title标签的名字：", beautiful_soup.title.name)
print("获取html的title标签的内容：", beautiful_soup.title.string)
print("获取html的title标签的内容：", beautiful_soup.title.get_text())
print("获取html的title标签的父标签：", beautiful_soup.title.parent.name)
print("获取html的h1标签：", beautiful_soup.h1)
print("获取html的h1标签的class：", beautiful_soup.h1['class'])
print("获取html的id是back的标签：", beautiful_soup.find(id='back'))
print("获取html的class是link的a标签：", beautiful_soup.find('a', {"class": "link"}))
print("获取html的所有a标签：", beautiful_soup.find_all('a'))
for link in beautiful_soup.find_all('a'):
    print("获取html的所有a标签的href：", link.get('href'))

print("获取html的所有名字是b开头的标签（正则匹配）：", beautiful_soup.find_all(re.compile('^b')))





